package com.ruoyi.web.controller.hmy;


import java.time.LocalDateTime;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.web.controller.tool.CommonUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hmy.domain.HmyShops;
import com.ruoyi.hmy.service.IHmyShopsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商户信息Controller
 *
 * @author ruoyi
 * @date 2023-04-15
 */
@RestController
@RequestMapping("/hmy/shops")
public class HmyShopsController extends BaseController {
    @Autowired
    private IHmyShopsService hmyShopsService;

    @Autowired
    private ISysConfigService configService;
    /**
     * 查询商户信息列表
     */
    @PreAuthorize("@ss.hasPermi('hmy:shops:list')")
    @GetMapping("/list")
    public TableDataInfo list(HmyShops hmyShops) {
        startPage();
        List<HmyShops> list = hmyShopsService.selectHmyShopsList(hmyShops);
        return getDataTable(list);
    }

    /**
     * 导出商户信息列表
     */
    @PreAuthorize("@ss.hasPermi('hmy:shops:export')")
    @Log(title = "商户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HmyShops hmyShops) {
        List<HmyShops> list = hmyShopsService.selectHmyShopsList(hmyShops);
        ExcelUtil<HmyShops> util = new ExcelUtil<HmyShops>(HmyShops.class);
        util.exportExcel(response, list, "商户信息数据");
    }

    /**
     * 获取商户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('hmy:shops:query')")
    @GetMapping(value = "/{shopId}")
    public AjaxResult getInfo(@PathVariable("shopId") Long shopId) {
        return success(hmyShopsService.selectHmyShopsByShopId(shopId));
    }



    /**
     * 新增商户信息
     */
    @PreAuthorize("@ss.hasPermi('hmy:shops:add')")
    @Log(title = "商户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HmyShops hmyShops) {
        hmyShops.setOpeningHours(hmyShops.getStartTime()+"/"+hmyShops.getEndTime());
//        hmyShops.setCity(StringUtils.join(hmyShops.getCitys(),"/"));
        hmyShops.setSalesCount(0L);
        hmyShops.setOpenStatus("3");
        hmyShops.setIsOpen("2");
        hmyShops.setPassed("1");
        hmyShops.setShopStatus("1");
        hmyShops.setApplyStatus("0");
        hmyShops.setTotalAmount("0");
        hmyShops.setUnliquidatedAmount("0");
        //获取平台收取费率
        final SysConfig sysConfig = configService.selectConfigById(CommonUtil.SYSCONFIGPLATFORMRATEID);
        //设置平台费率
        hmyShops.setPlatformRate(Integer.valueOf(sysConfig.getConfigValue()));
        hmyShops.setCreateTime(LocalDateTime.now());
        hmyShops.setUpdateTime(LocalDateTime.now());
        return toAjax(hmyShopsService.insertHmyShops(hmyShops));
    }

    /**
     * 修改商户信息
     */
    @PreAuthorize("@ss.hasPermi('hmy:shops:edit')")
    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HmyShops hmyShops) {
        hmyShops.setUpdateTime(LocalDateTime.now());
        return toAjax(hmyShopsService.updateHmyShops(hmyShops));
    }

    /**
     * 删除商户信息
     */
    @PreAuthorize("@ss.hasPermi('hmy:shops:remove')")
    @Log(title = "商户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable Long[] shopIds) {
        return toAjax(hmyShopsService.deleteHmyShopsByShopIds(shopIds));
    }
}
